home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 December / december_2000.iso / Intercd / root / Mail / ^MailShield / setup.exe / data1.cab / Default_Rule_Files / data.mml < prev    next >
Encoding:
Text File  |  2000-10-18  |  16.3 KB  |  456 lines

  1.  
  2. ##############################################################################
  3. # data.mml 
  4. #
  5. # MailShield script that is run after the SMTP DATA command ends
  6. # At this point, we have everything we need to decide whether to allow
  7. # or reject the message. The sender is expect either an OK or a refusal
  8. # at this point.
  9. #
  10.  
  11.  
  12. ##############################################################################
  13. # check to see if we should canonize the From: and Reply-To: addresses.
  14.  
  15.     if (scalar(@canonize) > 0) { 
  16.         # canonize the From header, if one exists in this message
  17.         if (index(lc(&HeaderGet("from")), @canonize) > -1) { 
  18.             &HeaderSet("From", &Canonize(&HeaderGet("from")));
  19.             $canonized = 1;
  20.         };
  21.  
  22.         # canonize the Reply-To header, if one exists in this message
  23.         if (index(lc(&HeaderGet("reply-to")), @canonize) > -1) { 
  24.             &HeaderSet("Reply-To", &Canonize(&HeaderGet("reply-to")));
  25.             $canonized = 1;
  26.         };
  27.         
  28.         # commit the changed header
  29.         if ($canonized == 1) {
  30.             $Header = &HeaderBuild;
  31.             &CommitHeader;
  32.         };
  33.     };
  34.  
  35.  
  36. ##############################################################################
  37. # If an email address has been designated to receive BCC copies of all incoming
  38. # mail, then send it now. 
  39.  
  40.     if (length($bcc_all_backup) > 0) {
  41.         &SendBccNow($bcc_backup, "", "");
  42.     };
  43.  
  44.  
  45. #############################################################################
  46. # (optional) Append SMTP transfer info, which normally would not be 
  47. # visible by the message recipient. Useful for later changing the rules.
  48.  
  49.     # You can append headers as shown below.  Be sure to call &CommitHeader;
  50.     # after any changes with the &Header... calls to commit your changes.
  51.     if ($append_smtp_info) {
  52.         $Header .= "\nX-SMTP-HELO: ".$SmtpHelo.
  53.                    "\nX-SMTP-MAIL-FROM: ".$SmtpMailFrom.
  54.                    "\nX-SMTP-RCPT-TO: ".join(",", @SmtpRcptTo).
  55.                    "\nX-SMTP-PEER-INFO: ".$PeerHostname." [".$PeerTcpip."]";
  56.         &CommitHeader;
  57.     };
  58.  
  59.  
  60. ##############################################################################
  61. # check for text in the To: header that we always accept mail for
  62.  
  63.     if (scalar(@ok_to) > 0) { 
  64.         if (regindex(lc(&HeaderGet("to")), @ok_to) > -1) { 
  65.             $accept = TRUE;
  66.         };
  67.     };
  68.  
  69.  
  70.  
  71. ##############################################################################
  72. # If MailShield mail merge support is enabled, check for the merge code, and 
  73. # if it is present, BCC this message to all its recipients, performing mail
  74. # merging while sending. Then set the response message, and refuse/exit so
  75. # that no further processing of this message occurs.
  76.     if ($enable_mail_merge) {
  77.         if (index(@Data, "<!-- merge") > -1) {
  78.             $response = &BccWithMailMerge ( $SmtpMailFrom, @Data, @SmtpRcptTo, $smtp_server);
  79.             &LogMessage($response." (mail merge enabled message detected)");
  80.             &Message($response);
  81.             &Refuse;
  82.             exit;
  83.         };
  84.     };
  85.  
  86.  
  87.  
  88. ##############################################################################
  89. # If this host has been accepted for relaying in begin.mml, the perform no more tests
  90.  
  91.     if ($accept) {
  92.         exit;
  93.     };
  94.  
  95.  
  96. ##
  97. ## Message header tests
  98. ##
  99.  
  100. ##############################################################################
  101. # Sleep for a number of seconds if we have been instructed to tarpit this connection.
  102.  
  103.     if ($tarpit) {
  104.         sleep($tarpit_delay);
  105.     };
  106.  
  107.  
  108. ##############################################################################
  109. # check to see if a From: address is missing
  110.  
  111.     if ($reject_no_from) {
  112.         if ((!&HeaderExists("from")) || (length(&ExtractEmailAddress(&HeaderGet("from"))) == 0)) { 
  113.             $smtp_message = "550 Body From: is missing or empty";
  114.             $log_message = "550 Body From: is missing or empty";
  115.             &DefaultRejection;
  116.         };
  117.     };
  118.  
  119. ##############################################################################
  120. # check to see if a banned X-Mailer header
  121.  
  122.     if (scalar(@banned_xmailer) > 0) { 
  123.         if (&HeaderExists("x-mailer")) { 
  124.             if (regindex(lc(&HeaderGet("x-mailer")), @banned_xmailer) > -1) { 
  125.                 $smtp_message = "550 Mail refused";
  126.                 $log_message = "550 Banned X-Mailer: text was found: '".$match."'";
  127.                 &DefaultRejection;
  128.             };
  129.         };
  130.     };
  131.  
  132.  
  133. ##############################################################################
  134. # check to see if a Subject: address is missing
  135.  
  136.     if ($reject_no_subject) { 
  137.         if (!&HeaderExists("subject")) { 
  138.             $smtp_message = "550 Body Subject: is missing or empty";
  139.             $log_message = "550 Body Subject: is missing or empty";
  140.             &DefaultRejection;
  141.         };
  142.     };
  143.  
  144. ##############################################################################
  145. # check to see if a Date: header is missing
  146.  
  147.     if ($reject_no_date) { 
  148.         if (!&HeaderExists("date")) { 
  149.             $smtp_message = "550 Body Date: is missing or empty";
  150.             $log_message = "550 Body Date: is missing or empty";
  151.             &DefaultRejection;
  152.         };
  153.     };
  154.  
  155.  
  156. ##############################################################################
  157. # check to see if a To: address is missing
  158.  
  159.     if ($reject_no_to) { 
  160.         if (!&HeaderExists("to")) { 
  161.             $smtp_message = "550 Body To: is missing or empty";
  162.             $log_message = "550 Body To: is missing or empty";
  163.             &DefaultRejection;
  164.         };
  165.     };
  166.  
  167. ##############################################################################
  168. # check to see if this From: address has been banned
  169.  
  170.     if (regindex(lc(&HeaderGet("from")), @banned_from) > -1) { 
  171.         $smtp_message = "550 Body From: is invalid";
  172.         $log_message = "550 Body From: of '".&HeaderGet("from")."' is not allowed / matched ".$match;
  173.         &DefaultRejection;
  174.     };
  175.  
  176. ##############################################################################
  177. # check to make sure that the From: address appears to be in a valid syntax,
  178. # that it have a domain name, and that the domain name is valid.
  179.  
  180.     if ($reject_invalid_from) { 
  181.         if (!&EmailAddressValid( &HeaderGet("from") )) { 
  182.             $smtp_message = "550 Your Body From: is invalid";
  183.             $log_message = "550 Body From: of ".&HeaderGet("from")." is not valid";
  184.             &DefaultRejection;
  185.         };
  186.     };
  187.  
  188.  
  189. ##############################################################################
  190. # check to make sure that the To: address appears to be in a valid syntax,
  191. # that it have a domain name, and that the domain name is valid.
  192.  
  193.     if ($reject_invalid_to) { 
  194.         if (!&EmailAddressValid( &HeaderGet("to") )) { 
  195.             $smtp_message = "550 Your Body To: is invalid";
  196.             $log_message = "550 Body To: of ".&HeaderGet("to")." is not valid";
  197.             &DefaultRejection;
  198.         };
  199.     };
  200.  
  201.  
  202. ##############################################################################
  203. # Check to see if any banned text appears anywhere in the header.
  204.  
  205.     if (scalar(@banned_header) > 0) { 
  206.         if (regindex(lc($Header), @banned_header) > -1) { 
  207.             $smtp_message = "550 Mail refused";
  208.             $log_message = "550 Banned text appeared in header: '".$match."'";
  209.             &DefaultRejection;
  210.         };
  211.     };
  212.  
  213. ##############################################################################
  214. # Check for maximum size of message
  215.  
  216.     if ($DataBytes > $max_message_size) {
  217.         $smtp_message = "550 Message too large";
  218.         $log_message = "550 Maximum body size of ".$max_message_size." bytes exceeded";
  219.         &DefaultRejection;
  220.     };
  221.  
  222. ##############################################################################
  223. # Check for maximum number of message lines
  224.  
  225.     if ($DataLines > $max_message_lines) {
  226.         $smtp_message = "550 Message too long";
  227.         $log_message = "550 Maximum body line count of ".$max_message_lines." exceeded";
  228.         &DefaultRejection;
  229.     };
  230.  
  231.  
  232. ##############################################################################
  233. # Check for maximum Received: HELO text line size
  234.  
  235.     if ($max_recv > 0) {
  236.         @array_helo = &extract(/from [^ ]*/, &HeaderGetAll("received"));
  237.         if (&LongestLengthInArray(@array_helo) > ($max_recv + 5)) {
  238.             $smtp_message = "550 Message rejected";
  239.             $log_message = "550 Maximum Received header line size of ".$max_recv." characters exceeded, found text: ".$match;
  240.             &DefaultRejection;
  241.         };
  242.     };
  243.  
  244. ##############################################################################
  245. # Check for HELO field tampering, by looking for ()[] in the "Received: from 'hostname'"
  246.  
  247.     if ($helo_tampering) {
  248.         @array_helo = &extract(/from [^ ]*/, &HeaderGetAll("received"));
  249.         push (@to_find, "(");
  250.         push (@to_find, ")");
  251.         push (@to_find, "[");
  252.         push (@to_find, "]");
  253.         if (regindex(@array_helo, @to_find) > -1) {
  254.             $smtp_message = "550 Message rejected";
  255.             $log_message = "550 HELO field tampering detected, found ".$match." in 'Received: from ...' HELO field" ;
  256.             &DefaultRejection;
  257.         };
  258.     };
  259.  
  260.  
  261. ##############################################################################
  262. # Check for banned text in the filenames of MIME attachments
  263.  
  264. if (scalar(@banned_attachment_filenames) > 0) {
  265.         @mime_array = &HeaderMimeGetAllArray;
  266.         @array_files = &extract(/name="?[^"]+"?/, join("\n", @mime_array));
  267.         if (regindexlc(@array_files, @banned_attachment_filenames) > -1) {
  268.         $smtp_message = "550 Message rejected";
  269.         $log_message = "550 Banned filename text of ".$match." found";
  270.         &DefaultRejection;
  271.     };
  272. };
  273.  
  274. ##############################################################################
  275. # Check for maximum length of filenames in MIME attachments
  276.  
  277.     if ($max_attachment_filename_length > 0) { 
  278.         @mime_array = &HeaderMimeGetAllArray;
  279.         @array_files = &extract(/filename="[^"]*"/, join("\n", @mime_array));
  280.         if (&LongestLengthInArray(@array_files) > ($max_attachment_filename_length + 11)) {
  281.             $smtp_message = "550 Message rejected";
  282.             $log_message = "550 Maximum filename attachment size of ".$max_attachment_filename_length." reached for: ".$match;
  283.             &DefaultRejection;
  284.         };
  285.     };
  286.  
  287.  
  288. ##############################################################################
  289. # Check for maximum number of recipients in the TO: CC: and BCC: headers
  290.  
  291.     if (&CountRecipients > $max_recipients) {
  292.         $smtp_message = "550 Message has too many recipients";
  293.         $log_message = "550 Maximum recipient count of ".$max_recipients." exceeded, counted ".&CountRecipients." recipients in this message";
  294.         &DefaultRejection;
  295.     };
  296.  
  297. ##############################################################################
  298. # Check for forged date header
  299.  
  300.     if ($reject_forged_date_header) { 
  301.         push(@forged_date_headers, "-0600 (EST)");
  302.         if (index(&HeaderGet("date"), @forged_date_headers) > -1) { 
  303.             $smtp_message = "550 Mail refused";
  304.             $log_message = "550 Forged Date: header text was detected: '".$match."'";
  305.             &DefaultRejection;
  306.         };
  307.     };
  308.  
  309.  
  310.  
  311. ##############################################################################
  312. # Check for forged message id
  313.  
  314.     if ($reject_forged_message_id_header) {
  315.         push(@forged_messageid_headers, "AAA000000.00000");
  316.         push(@forged_messageid_headers, "Mach10");
  317.  
  318.         if (index(&HeaderGet("message-id"), @forged_messageid_headers) > -1) { 
  319.             $smtp_message = "550 Mail refused";
  320.             $log_message = "550 Forged Message-Id: header text was detected: '".$match."'";
  321.             &DefaultRejection;
  322.         };
  323.  
  324.         push(@forged_messageid_regep, "RAF[0-9]+\.[0-9]+_[0-9]+");
  325.         push(@forged_messageid_regep, ".*>.*>");
  326.         if (&HeaderGet("message-id") =~ @forged_messageid_regep) { 
  327.             $smtp_message = "550 Mail refused";
  328.             $log_message = "550 Forged Message-Id: header text was detected: '".$match."'";
  329.             &DefaultRejection;
  330.         };
  331.     };
  332.  
  333. ##############################################################################
  334. # Check for unwanted Subject text
  335.  
  336.     if (scalar(@banned_subject) > 0) { 
  337.         if (regindex(lc(&HeaderGet("subject")), @banned_subject) > -1) { 
  338.             $smtp_message = "550 Mail refused";
  339.             $log_message = "550 Banned Subject: text was found: '".$match."'";
  340.             &DefaultRejection;
  341.         };
  342.     };
  343.  
  344. ##############################################################################
  345. # Check for unwanted Subject prefix
  346.  
  347.     if (scalar(@banned_subject_prefix) > 0) { 
  348.         if (regindex(lc(&HeaderGet("subject")), @banned_subject_prefix) == 0) { 
  349.             $smtp_message = "550 Mail refused";
  350.             $log_message = "550 Banned Subject: prefix was found: '".$match."'";
  351.             &DefaultRejection;
  352.         };
  353.     };
  354.  
  355.  
  356. ##############################################################################
  357. # Check for unwanted To: text
  358.  
  359.     if (scalar(@banned_to) > 0) { 
  360.         if (regindex(lc(&HeaderGet("to")), @banned_to) > -1) { 
  361.             $smtp_message = "550 Mail refused";
  362.             $log_message = "550 Banned To: text was found: '".$match."'";
  363.             &DefaultRejection;
  364.         };
  365.     };
  366.  
  367. ##############################################################################
  368. # Check for unwanted Received: text
  369.  
  370.     if (scalar(@banned_recv) > 0) {    
  371.         @received = &HeaderGetArray("received");
  372.         lc(@received);
  373.         if (regindex(@received, @banned_recv) > -1) { 
  374.             $smtp_message = "550 Mail refused";
  375.             $log_message = "550 Banned Received: text was found: '".$match."'";
  376.             &DefaultRejection;
  377.         };
  378.     };
  379.  
  380.  
  381. ##
  382. ## Message body tests
  383. ##
  384.  
  385. ##############################################################################
  386. # check to see if any banned text appears anywhere (header or body)
  387.  
  388.     if ($DataBytes < $large_passthrough) {
  389.         if (scalar(@banned_text) > 0) {    
  390.             if (regindexlc(@Data, @banned_text) > -1) { 
  391.                 $smtp_message = "550 Mail refused";
  392.                 $log_message = "550 Banned text appeared in header or body: '".$match."'";
  393.                 &DefaultRejection;
  394.             };
  395.         };
  396.     };
  397.  
  398.  
  399. ##
  400. ## Final processing of this message
  401. ##
  402.  
  403. ##############################################################################
  404. # Check if this message should be sent to a backup, rather than rejected
  405.  
  406.     if ($send_to_backup) {
  407.         &SendMessageToBackup;
  408.         $send_to_backup = FALSE;
  409.     };
  410.  
  411.  
  412. # optional steps follow, move them before the "final processing" step if 
  413. # you want to use them
  414.  
  415. #############################################################################
  416. # Modify the entire message as lines
  417. #
  418. # The @Data array holds every line in the message.  You can change this as 
  419. # you see fit. Any changes you make to this array will be reflected in the 
  420. # message that MailShield delivers
  421.  
  422. #@Data =~ s/shelby.com/tile.net/;
  423. #&CommitData;
  424.  
  425.  
  426. #############################################################################
  427. # Modify the header
  428. #
  429. # You can modify the header of the message by either appending the $Header
  430. # variable and then calling &CommitHeaderText; or by calls to HeaderSet, 
  431. # HeaderAppend and HeaderDeleteKey followed by a call to &CommitHeaderCalls 
  432. # to save the changes made by those function calls.
  433.  
  434. #$Header .= "\nX-Test: test test";
  435. #&CommitHeaderText;
  436.  
  437. #&HeaderAppend("X-Local-Comment", "Received by MailShield");
  438. #&CommitHeaderCalls;
  439.  
  440.  
  441. #############################################################################
  442. # The print statement can be useful for debugging
  443.  
  444. #print "Passed through data...";
  445.  
  446.     
  447. #############################################################################
  448. # Change the recipients
  449. #
  450. # The @SmtpRcptTo array variable holds all the recipient addresses of this 
  451. # message. You can change this as you see fit, and append additional recipients
  452. # if need be.  No "commit" statement is needed to make changes appear.
  453.  
  454. #@SmtpRcptTo =~ s/shelby.com/tile.net/;
  455.     
  456.